package com.sin.j2me.util;

public class Map {
	private String[] keys = null;
	private Object[] values = null;
	private int top;
	public static int DEFAULT_CAPACITY = 10;
	public Map() {
		this(DEFAULT_CAPACITY);
	}
	
	public Map(int capacity){
		this.keys = new String[capacity];
		this.values = new Object[capacity];
		this.top = 0;
	}
	public int put(String key, Object obj){
		int ix = indexOf(key);
		if(ix<0){
			if(full()){
				newCapacity();
			}
			this.keys[this.top] = key;
			this.values[this.top] = obj;
			++this.top;
			return this.top;
		}
		else{
			this.values[ix] = obj;
			return ix;
		}
	}
	public int indexOf(String key){
		for(int i=0; i<this.top; ++i){
			if(this.keys[i].equals(key)){
				return i;
			}
		}
		return -1;
	}
	public Object get(String key){
		int ix = indexOf(key);
		return get(ix);
	}
	public Object get(int ix){
		if(ix<0 || ix>=this.top)
			return null;
		else
			return this.values[ix];
	}
	public String getKey(int ix){
		return this.keys[ix];
	}
	public boolean empty() {
		return this.top<=0;
	}
	
	public boolean full() {
		return this.values.length<=this.top;
	}
	public int size(){
		return this.top;
	}
	private int newCapacity(){
		int newlen = this.values.length * 2;
		String[] srckeys = this.keys;
		Object[] srcvals = this.values;
		this.keys = new String[newlen];
		this.values = new Object[newlen];
		System.arraycopy(srckeys, 0, keys, 0, srcvals.length);
		System.arraycopy(srcvals, 0, values, 0, srcvals.length);
		return newlen;
	}
}
